package com.link2room.bumblebee.facade.impl;

import com.link2room.bumblebee.dto.param.base.DeleteWebSettingParamDto;
import com.link2room.bumblebee.dto.param.base.WebSettingCreateParamDto;
import com.link2room.bumblebee.dto.param.base.FindWebSettingParamDto;
import com.link2room.bumblebee.dto.param.base.WebSettingUpdateParamDto;
import com.link2room.bumblebee.dto.result.base.WebSettingsDto;
import com.link2room.bumblebee.entity.WebSettingsEntity;
import com.link2room.bumblebee.facade.IWebSettingManagerFacadeService;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.link2room.enums.exception.BumblebeeSysExceptionEnum.CREATE_WEBSETTINGS_ERROR;
import static com.link2room.enums.exception.BumblebeeSysExceptionEnum.UPDATE_WEBSETTINGS_ERROR;


/**
 * Created by LN on 2017/12/18
 */
@Component("webSettingManagerFacadeService")
public class WebSettingManagerFacadeServiceImpl implements IWebSettingManagerFacadeService {
    @Resource
    private ICommonService commonService;
    @Override
    public QueryResult<WebSettingsDto> listWebSettingsDto(FindWebSettingParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode,"[lhotelGroupCode] is required.");
        String lhotelCode = paramDto.getLhotelCode();
        String appid = paramDto.getAppid();
        String componentAppid = paramDto.getComponentAppid();
        String domain = paramDto.getDomain();
        String name = paramDto.getName();
        WebSettingsEntity find = new WebSettingsEntity();
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setLhotelCode(lhotelCode);
        find.setAppid(appid);
        find.setComponentAppid(componentAppid);
        find.setDomain(domain);
        find.setName(name);
        find.setCode(paramDto.getCode());
        QueryResult<WebSettingsEntity> qres = commonService.findAllEQ(WebSettingsEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        return ClassConverUtil.copyQueryResultToDtoQueryResult(qres,WebSettingsDto.class,true);
    }

    @Override
    public void createWebSettings(WebSettingCreateParamDto paramDto) throws Exception {
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code,"[code] is required.");
        L2RAssert.isTrue(countCode(code) > 0,new SysException(CREATE_WEBSETTINGS_ERROR,"域名重复[code] is "+code));
        String appid = paramDto.getAppid();
//        L2RAssert.stringBlank(appid,"[appid] is required.");

        String domain = paramDto.getDomain();
        L2RAssert.stringBlank(domain,"[domain] is required.");
        L2RAssert.isTrue(countDomain(domain) > 0,new SysException(CREATE_WEBSETTINGS_ERROR,"域名重复[domain] is "+domain));
        String cssExtra = paramDto.getCssExtra();
        String logo = paramDto.getLogo();
        String name = paramDto.getName();
        L2RAssert.stringBlank(name,"[name] is required.");
        String componentAppid = paramDto.getComponentAppid();
        String statisticsId = paramDto.getStatisticsId();
        WebSettingsEntity webSettingsEntity = new WebSettingsEntity();
        webSettingsEntity.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        webSettingsEntity.setLhotelCode(paramDto.getLhotelCode());
        webSettingsEntity.setDomain(domain);
        webSettingsEntity.setCssExtra(cssExtra);
        webSettingsEntity.setName(name);
        webSettingsEntity.setComponentAppid(componentAppid);
        webSettingsEntity.setAppid(appid);
        webSettingsEntity.setStatisticsId(statisticsId);
        webSettingsEntity.setLogo(logo);
        webSettingsEntity.setCode(code);
        webSettingsEntity.setWxQrCodeImg(paramDto.getWxQrCodeImg());
        webSettingsEntity.setICP(paramDto.getICP());
        commonService.save(webSettingsEntity);
    }

    @Override
    public void updateCreateWebSettings(WebSettingUpdateParamDto paramDto) throws Exception {
        String appid = paramDto.getAppid();
        L2RAssert.stringBlank(appid,"[appid] is required.");
        String componentAppid = paramDto.getComponentAppid();
        String cssExtra = paramDto.getCssExtra();
        String domain = paramDto.getDomain();
        L2RAssert.stringBlank(domain,"[domain] is required.");
        String logo = paramDto.getLogo();
        String name = paramDto.getName();
        L2RAssert.stringBlank(name,"[name] is required.");
        String statisticsId = paramDto.getStatisticsId();
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        String lhotelCode = paramDto.getLhotelCode();
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code,"[code] is code.");
        String isHalt = paramDto.getIsHalt();
        WebSettingsEntity find = new WebSettingsEntity();
        find.setCode(code);
        WebSettingsEntity qres = commonService.findOneEQ(WebSettingsEntity.class, find);
        L2RAssert.isTrue(null == qres,new SysException(UPDATE_WEBSETTINGS_ERROR,"站点信息不存在[code] is"+code));
        if(!qres.getDomain().equals(domain)){
            L2RAssert.isTrue(countDomain(domain) > 0,new SysException(UPDATE_WEBSETTINGS_ERROR,"域名重复[domain] is "+domain));
        }
        qres.setAppid(appid);
        qres.setComponentAppid(componentAppid);
        qres.setCssExtra(cssExtra);
        qres.setDomain(domain);
        qres.setLogo(logo);
        qres.setName(name);
        qres.setStatisticsId(statisticsId);
        qres.setLhotelCode(lhotelCode);
        qres.setLhotelGroupCode(lhotelGroupCode);
        qres.setIsHalt(isHalt);
        qres.setICP(paramDto.getICP());
        qres.setWxQrCodeImg(paramDto.getWxQrCodeImg());
        commonService.merge(qres);
    }

    @Override
    public WebSettingsDto findWebSettingsDto(FindWebSettingParamDto paramDto) throws Exception {
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code,"[code] is required");
        WebSettingsEntity find = new WebSettingsEntity();
        find.setCode(code);
        WebSettingsEntity qres = commonService.findOneEQ(WebSettingsEntity.class, find);
        return ClassConverUtil.copyPropertiesToDto(qres,WebSettingsDto.class,true);
    }

    @Override
    public void deleteWebSettings(DeleteWebSettingParamDto paramDto) throws Exception {
        String code = paramDto.getCode();
        L2RAssert.stringBlank(code,"[code] is required.");
        WebSettingsEntity find = new WebSettingsEntity();
        find.setCode(code);
        WebSettingsEntity qres = commonService.findOneEQ(WebSettingsEntity.class, find);
        if(qres == null){return;}
        commonService.deleteAllEQ(WebSettingsEntity.class,qres);
    }

    private long countDomain(String domain) throws Exception {
        WebSettingsEntity c = new WebSettingsEntity();
        c.setDomain(domain);
        return commonService.getCountAllEQ(WebSettingsEntity.class, c, null);
    }
    private long countCode(String code) throws Exception {
        WebSettingsEntity c = new WebSettingsEntity();
        c.setCode(code);
        return commonService.getCountAllEQ(WebSettingsEntity.class, c, null);
    }
}
